<?php
namespace App\Services\Admin;

use App\Repositories\Admin\JobRepository;
use App\Repositories\Admin\StaffRepository;
use App\Repositories\Admin\UserImgTmpRepository;
use App\Repositories\Admin\UserRepository;

class StaffService
{
    private $service;
    private $user;

    public function __construct()
    {
        $this->service = new StaffRepository();
        $this->user = new UserRepository();
    }

    public function getIndex()
    {
        return $this->service->getIndexList();
    }

    /**
     * 电脑端首页
     */
    public function getWebIndex()
    {
        $models = $this->service->getWebIndexList();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $k=>$model) {
            $userModel = $this->user->getShowOne($model->uid);
            $head = $userModel ? $userModel->head : '';
            $dataArr[$k] = [
                'staffId' => $model->id,
                'staffName' => $model->name,
                'skillDesc' => $model->introducer,
                'imgUrl' => $head,
            ];
        }
        return $dataArr;
    }

    public function add($data,$uid)
    {
        if (isset($data['image'])) { $this->setImgTmp($data['image']); }
        $model = $this->service->getShowOneByUid($uid);
        if (!$model) {
            return $this->service->setAdd($data);
        } else {
            return $this->service->setModifyByUid($data,$uid);
        }
    }

    public function modify($data,$id)
    {
        return $this->service->setModify($data,$id);
    }

    public function modifyByUid($data,$uid)
    {
        return $this->service->setModifyByUid($data,$uid);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    public function getOneByUid($uid)
    {
        return $this->service->getShowOneByUid($uid);
    }

    public function getEduName($edu)
    {
        return $this->service->getEduNameByEdu($edu);
    }

    /** 政治面貌
     */
    public function getPolityName($polity)
    {
        return $this->service->getPolityNameByPolity($polity);
    }

    public function getMarryName($marry)
    {
        return $this->service->getMarryNameByMarry($marry);
    }

    public function getSkills($uid)
    {
        $model = $this->service->getShowOneByUid($uid);
        $skills = $this->service->getSkillList();
        $skillStrs = ($model&&$model->skill) ? explode(',',$model->skill) : array();
        $dataArr = array();
        foreach ($skills as $k=>$skill) {
            $dataArr[$k]['id'] = $skill['id'];
            $dataArr[$k]['name'] = $skill['name'];
            if (in_array($skill['id'],$skillStrs)) {
                $dataArr[$k]['checked'] = 1;
            } else {
                $dataArr[$k]['checked'] = 0;
            }
        }
        return $dataArr;
    }

    public function getHobbys($uid)
    {
        $model = $this->service->getShowOneByUid($uid);
        $hobbys = $this->service->getHobbyList();
        $hobbyStrs = ($model&&$model->hobby) ? explode(',',$model->hobby) : array();
        $dataArr = array();
        foreach ($hobbys as $k=>$hobby) {
            $dataArr[$k]['id'] = $hobby['id'];
            $dataArr[$k]['name'] = $hobby['name'];
            if (in_array($hobby['id'],$hobbyStrs)) {
                $dataArr[$k]['checked'] = 1;
            } else {
                $dataArr[$k]['checked'] = 0;
            }
        }
        return $dataArr;
    }

    /**
     * 用户类型名称
     */
    public function getCateName($cate)
    {
        return $this->service->getCateNameByCate($cate);
    }


    /**
     * -------------------------------------
     * 以下是信息2，选填信息
     * -------------------------------------
     */

    /**
     * 获取一条可选记录
     */
    public function getOptionByUid($uid)
    {
        return $this->service->getOneOptionByUid($uid);
    }

    /**
     * 员工公司选项
     */
    public function getComRadios()
    {
        return $this->service->getComRadioList();
    }

    /**
     * 员工公司选项，更新
     */
    public function setOptionModify($uid,$cate,$data)
    {
        return $this->service->setOptionModifyByUid($uid,$cate,$data);
    }

    /**
     * -------------------------------------
     * 选填信息
     * -------------------------------------
     */


    /**
     * -------------------------------------
     * 求职意向
     * -------------------------------------
     */

    /**
     * 公司求职意向，获取
     */
    public function getComWill($uid)
    {
        return $this->service->getOneComWill($uid);
    }

    /**
     * 公司求职意向，设置
     */
    public function setComWill($uid,$data)
    {
        return $this->service->setComWillByUid($uid,$data);
    }

    /**
     * 工厂求职意向，获取
     */
    public function getFacWill($uid)
    {
        return $this->service->getOneFacWill($uid);
    }

    /**
     * 工厂求职意向，设置
     */
    public function setFacWill($uid,$data)
    {
        return $this->service->setFacWillByUid($uid,$data);
    }

    /**
     * -------------------------------------
     * 求职意向
     * -------------------------------------
     */





    /**
     * -------------------------------------
     * 员工公司认证
     * -------------------------------------
     */

    /**
     * 员工认证，获取
     */
    public function getComFile($uid)
    {
        return $this->service->getComFileList($uid);
    }

    /**
     * 员工认证，设置
     */
    public function setComFile($uid,$fileKey='selfs',$filePath='')
    {
        $dataArr = array();
        $keys = array('selfs','idcards','byzs','awards','marrys','drives',);
        $resFiles = $this->service->getComFileJson($uid);
        if (!$resFiles) {
            foreach ($keys as $key) {
                if ($fileKey==$key) {
                    $dataArr[$key][] = $filePath;
                } else {
                    $dataArr[$key] = array();
                }
            }
            $this->service->setComFileAddByUid($uid,$dataArr);
            return true;
        }
        $resFiles = json_decode($resFiles,true);
        foreach ($keys as $key) {
            $dataArr[$key] = $resFiles[$key];
            if ($fileKey==$key) {
                $dataArr[$key][] = $filePath;
            }
        }
        $this->service->setComFileByUid($uid,$dataArr);
        return true;
    }

    /**
     * 员工认证，链接设置
     */
    public function setComFileLink($uid,$data)
    {
        /*
        if ($selfs=$data['selfs']) {
            foreach ($selfs as $self) { $this->setImgTmp($self); }
        }
        if ($idcards=$data['idcards']) {
            foreach ($idcards as $idcard) { $this->setImgTmp($idcard); }
        }
        if ($byzs=$data['byzs']) {
            foreach ($byzs as $byz) { $this->setImgTmp($byz); }
        }
        if ($awards=$data['awards']) {
            foreach ($awards as $award) { $this->setImgTmp($award); }
        }
        if ($marrys=$data['marrys']) {
            foreach ($marrys as $marry) { $this->setImgTmp($marry); }
        }
        if ($drives=$data['drives']) {
            foreach ($drives as $drive) { $this->setImgTmp($drive); }
        }*/
        return $this->service->setComFileByUid($uid,$data);
    }

    /**
     * -------------------------------------
     * 员工公司认证
     * -------------------------------------
     */





    /**
     * -------------------------------------
     * 员工职位
     * -------------------------------------
     */
    public function getJobs($cate)
    {
        $jobService = new JobService();
        $models = $jobService->getJobsByCate($cate);
        foreach ($models as $model) {
            $model->subs = $jobService->getJobsByPid($model->pid);
        }
        return $models;
    }

    /**
     * APP获取简历
     */
    public function getResume($uid)
    {
        $userModel = new UserRepository();
        $jobModel = new JobRepository();
        $user = $userModel->getShowOne($uid);
        $staff = $this->service->getShowOneByUid($uid);
        if (!$staff) { return ''; }
        $job = $jobModel->getShowOne($staff->job);
        if ($user->head && file_exists(ltrim($user->head,'/'))) {
            $head = $user->head;
        } else {
            $head = '';
        }
        if ($staff->image && file_exists(ltrim($staff->image,'/'))) {
            $image = $staff->image;
        } else {
            $image = '';
        }
        $skillStrNew = '';
        if ($skillStr=$staff->skill) {
            $skills = explode(',',$skillStr);
            $skillArr = array();
            foreach ($skills as $k => $v) {
                $skillName = $this->service->getCateNameById($v);
                $skillArr[$k] = $skillName;
            }
            $skillStrNew = implode('，',$skillArr);
        }
        $hobbyStrNew = '';
        if ($hobbyStr=$staff->hobby) {
            $hobbys = explode(',',$hobbyStr);
            $hobbyArr = array();
            foreach ($hobbys as $k => $v) {
                $hobbyName = $this->service->getCateNameById($v);
                $hobbyArr[$k] = $hobbyName;
            }
            $hobbyStrNew = implode('，',$hobbyArr);
        }
        $jobName = $job ? $job->name : '未知';
        $dataArr = array(
            //注册信息
            'uname' => $user->name,
            'mobile' => $user->phone,
            'head' => $head,
            'sexName' => $user->sex==1?'男':'女',
            'resume' => $user->is_resume, //简历是否被搜索
            //基本信息
//            'cateName' => $staff->cate==1?'工厂员工':'公司员工',
//            'realname' => $staff->realname,
            'birthday' => $staff->birthday,
            'eduName' => $this->service->getEduNameByEdu($staff->education),
            'weight' => $staff->weight,
//            'polityName' => $this->service->getPolityNameByPolity($staff->political_status),
//            'native_place' => $staff->native_place,
//            'nationality' => $staff->nationality,
            'marryName' => $this->service->getMarryNameByMarry($staff->marriage),
            'house' => $staff->house,
            'salary' => $staff->salary,
            'jobName' => $jobName,
            'image' => $image,
            'skill' => $skillStrNew,
            'hobby' => $hobbyStrNew,
        );
        return $dataArr;
    }

    /**
     * 设置简历是否被搜索
     */
    public function setResume($uid,$resume)
    {
        return $this->user->setResume($uid,$resume);
    }





    /**
     * 更新临时图片状态
     */
    public function setImgTmp($url)
    {
        $tmp = new UserImgTmpRepository();
        $tmp->setDelByUrl($url);
        return true;
    }
}